use dbproc
go
alter procedure spx_get_deposits
@OwnerAcc_id int,
@res varchar(max) output
as
begin
declare @xmlRes xml
	set @xmlRes = (
	select a.id, a.summary, isnull(m.FirstName,'')+isnull(m.LastName,'') as name, p.phone from dbdata.dbo.accounts as a
			join dbdata.dbo.account_categs as ac on categ_id = ac.id 
			join dbdata.dbo.users as u on u.id=a.uid
			join dbdata.dbo.phones as p on p.uid=u.id
			join Management.dbo.Users as m on u.name=m.ID
			where 
			p.as_default=1 and p.dt_removed is null and p.enabled=1
			and u.dt_locked is null and
			code='6' and a.enabled=1 and a.OwnerAcc_id=@OwnerAcc_id
			order by a.id for xml raw('deposit'), root('DepositList'), type
	);
	set @res=CONVERT(varchar(max),@xmlRes)
end;
go

grant execute on spx_get_deposits to Executer;

/*
DECLARE	
		@res varchar(max)

EXEC	dbproc.[dbo].[spx_get_deposits]
		@OwnerAcc_id = 5,
		@res = @res OUTPUT

SELECT	@res as N'@res'

*/
